/**
 * Created by sunyxq on 16/3/26.
 */
angular.module('hyt.receiptDaoZhanCtrl',[])
    .controller('DaoZhanReceiptListCtrl',function($scope,ZdListWqFactory,ZdListYqFactory,ZdListSearchFactory,$ionicPopup,ENV,$ionicLoading,Storage,$stateParams,$state,$timeout,$ionicTabsDelegate){
        $scope.showloading = true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var zd_id=Storage.get('zd_id');
        var bm_id=Storage.get('bm_id');
        var dw_id=Storage.get('dw_id');
        ZdListWqFactory.getZdWqList(sid,user_id,yh_id,zd_id,dw_id);
        ZdListYqFactory.getZdYqList(sid,user_id,yh_id,zd_id,dw_id);
        ZdListSearchFactory.getZdSearchList(sid,user_id,yh_id,zd_id,$stateParams.cph,$stateParams.sfz,$stateParams.kssj,$stateParams.jssj,dw_id);
        if (Storage.get('dzTab')==1){
            $timeout(function(){
                $ionicTabsDelegate.select(1);
            },300);
        }

        $scope.$on('zdListWq.zdUpdated', function() {
            var zdWqFactory=ZdListWqFactory.getWqZd();
            $scope.zdWqData = zdWqFactory.data;
            $scope.getClzt=function(clzt){
                switch (clzt){
                    case 1 :
                        $scope.clzt = '装车';
                        break;
                    case 2 :
                        $scope.clzt = '发车';
                        break;
                    case 3 :
                        $scope.clzt = '到货';
                        break;
                    default :
                        break;
                }
                return $scope.clzt;
            }
            $scope.showloading = false;

        });
        $scope.$on('zdListYq.zdUpdated', function() {
            var zdYqFactory=ZdListYqFactory.getYqZd();
            $scope.zdYqData = zdYqFactory.data;
            console.log($scope.zdYqData);
            $scope.getClzt=function(clzt){
                switch (clzt){
                    case 1 :
                        $scope.clzt = '装车';
                        break;
                    case 2 :
                        $scope.clzt = '发车';
                        break;
                    case 3 :
                        $scope.clzt = '到货';
                        break;
                    default :
                        break;
                }
                return $scope.clzt;
            }
            $scope.showloading = false;

        });
        $scope.$on('zdListSearch.zdUpdated', function() {
            var zdSearchFactory=ZdListSearchFactory.getSearchZd();
            $scope.zdSearchData = zdSearchFactory.data;
            console.log($scope.zdSearchData);
            $scope.getClzt=function(clzt){
                switch (clzt){
                    case 1 :
                        $scope.clzt = '装车';
                        break;
                    case 2 :
                        $scope.clzt = '发车';
                        break;
                    case 3 :
                        $scope.clzt = '到货';
                        break;
                    default :
                        break;
                }
                return $scope.clzt;
            }
            $scope.showloading = false;

        });
        $scope.doRefreshWq = function(){
            ZdListWqFactory.getZdWqList(sid,user_id,yh_id,zd_id,dw_id);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.loadMoreWq = function(){
            ZdListWqFactory.getMoreWqCyd(sid,user_id,yh_id,zd_id,dw_id);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        $scope.doRefreshYq = function(){
            ZdListYqFactory.getZdYqList(sid,user_id,yh_id,zd_id,dw_id);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.loadMoreYq = function(){
            ZdListYqFactory.getMoreYqCyd(sid,user_id,yh_id,zd_id,dw_id);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        $scope.selectTab=function(){
            Storage.set('dzTab',0);
            ZdListWqFactory.getZdWqList(sid,user_id,yh_id,zd_id,dw_id);
        };

        $scope.selectTab1=function(){
            Storage.set('dzTab',1);
            ZdListYqFactory.getZdYqList(sid,user_id,yh_id,zd_id,dw_id);
        };

        $scope.doRefreshSearch = function(){
            ZdListSearchFactory.getZdSearchList(sid,user_id,yh_id,zd_id,$stateParams.cph,$stateParams.sfz,$stateParams.kssj,$stateParams.jssj,dw_id);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.loadMoreSearch = function(){
            ZdListSearchFactory.getMoreSearchCyd(sid,user_id,yh_id,zd_id,$stateParams.cph,$stateParams.sfz,$stateParams.kssj,$stateParams.jssj,dw_id);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        $scope.hasNextPageWq =function(){
            return ZdListWqFactory.hasNextPageWq();
        }
        $scope.hasNextPageYq =function(){
            return ZdListYqFactory.hasNextPageYq();
        }
        $scope.hasNextPageSearch =function(){
            return ZdListSearchFactory.hasNextPageSearch();
        }
        $scope.doReceipt = function(data){
            var receipt = $ionicPopup.confirm({
                title : '确认签收',
                //template : '确认签收?'
                cancelText : '取消',
                cancelType : 'button-stable',
                okText : '确定'
            });


            receipt.then(function(r){
                if(r){
                    ZdListWqFactory.getReceipt(data.zcd_id,bm_id,data.qsrmc,sid,user_id,yh_id,dw_id);

                    $scope.$on('Receipt.ReceiptUpdate',function(){
                        $ionicLoading.show({
                            template : '签收成功',
                            duration : 600
                        });
                        ZdListWqFactory.getZdWqList(sid,user_id,yh_id,zd_id,dw_id);
                    });
                    $scope.$on('ErroReceipt.erroReceiptUpdate',function(){
                        $ionicLoading.show({
                            template : '签收失败',
                            duration : 600
                        });
                    });
                }else{
                    console.log('取消');
                }
            });
        }

    })

    .controller('DaoZhanDetailReceiptCtrl',function($scope,$state,ZdDetailFactory,$stateParams,$ionicPopup,$ionicLoading,Storage,$ionicHistory){
        $scope.loading = true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        var bm_id=Storage.get('bm_id');
        var zd_id=Storage.get('zd_id');
        var id = $stateParams['id'];
        var cydh=$stateParams['cydh'];
        console.log(id);
        $scope.cyd={
            cydh:''
        };

        ZdDetailFactory.getZdDetail(id,sid,user_id,yh_id,dw_id,zd_id,cydh);
        $scope.$on('zdDetail.detailUpdated',function(){
            var dzDetail = ZdDetailFactory.getDetail();
            $scope.ZdDetailData = dzDetail.data;
            $scope.getZt=function(cyzt){
                switch (cyzt){
                    case 1:
                        $scope.cyzt = "待受理";
                        break;
                    case 2:
                        $scope.cyzt = "已拒收";
                        break;
                    case 3:
                        $scope.cyzt = "已承运";
                        break;
                    case 4:
                        $scope.cyzt = "已装车";
                        break;
                    case 5:
                        $scope.cyzt = "已签收";
                        break;
                    case 6:
                        $scope.cyzt = "已撤销";
                        break;
                    case 10:
                        $scope.cyzt = "承运中";
                        break;
                    case 20:
                        $scope.cyzt = "已装车";
                        break;
                    case 30 :
                        $scope.cyzt = "中转";
                        break;
                    case 40:
                        $scope.cyzt = "外包";
                        break;
                    case 70:
                        $scope.cyzt = "到货签单";
                        break;
                    case 90:
                        $scope.cyzt = "已送货";
                        break;
                    case 100:
                        $scope.cyzt = "客户签收";
                        break;
                    case 110:
                        $scope.cyzt = "签单回收";
                        break;
                    case 120:
                        $scope.cyzt = "签单接收";
                        break;
                    case 130:
                        $scope.cyzt = "签单发放";
                        break;
                    case 140:
                        $scope.cyzt = "回单";
                        break;
                    case 150:
                        $scope.cyzt = "清单";
                        break;
                    default:
                        break;
                }
                return $scope.cyzt;
            }

            $scope.loading = false;
        });

        $scope.doRefresh = function(){
            ZdDetailFactory.getZdDetail(id,sid,user_id,yh_id,dw_id,zd_id,cydh);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.loadMore = function(){

            ZdDetailFactory.getMoreDetail(id,sid,user_id,yh_id,dw_id,zd_id,cydh);

            $scope.$broadcast('scroll.infiniteScrollComplete');
        };

        $scope.hasNextPage =function(){
            return ZdDetailFactory.hasNextPage();
        };

        var zcdData;

        $scope.doSingleReceipt = function(data){
            console.log(data);
            var singleReceipt = $ionicPopup.confirm({
                title : '确认签收',
                cancelText : '取消',
                cancelType : 'button-stable',
                okText : '确定'
            });


            var sdsl='';

            singleReceipt.then(function(r){
                console.log(r);
                if(r){
                    ZdDetailFactory.getSingleReceipt(sid,yh_id,user_id,dw_id,data.cyd_id,sdsl,bm_id,data.zcdmx_id);

                    $scope.$on('SingleReceipt.singleUpdate',function(){
                        $ionicLoading.show({
                            template : '签收成功',
                            duration : 600
                        });
                        $("#qs"+data.cyd_id).removeClass("show").addClass("hide");
                        $("#sx"+data.cyd_id).removeClass("show").addClass("hide");
                        $("#zt"+data.cyd_id).text("到货签单");
                    });

                    $scope.$on('rroSingleReceipt.erroSingleUpdate',function(){
                        $ionicLoading.show({
                            template : '签收失败',
                            duration : 600
                        });
                    });

                }else{
                    console.log('abcde');
                }
            });
        };
        $scope.hsdjdetail = function(cyd_id){
            $state.go('hsdjdetail',{cyd_id:cyd_id});
        };

        $scope.dzdetailSearch=function(){
            $state.go('dzdetailSearch',{id:id});
        };

        $scope.searchCyd = function(cydh){

            $state.go('dzdetail',{cydh:cydh,id:id});
        };

        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };
    })

    .controller('DaoZhanSearchCtrl',function($scope,$ionicLoading,$ionicPopup,$state,CarFactory,SfzFactory,Storage,$stateParams,$filter){
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        var zd_id=Storage.get('zd_id');
        var bmmc=$stateParams['bmmc'];
        $scope.bmmc=bmmc;
        //CarFactory.getCphList(sid,user_id,yh_id,dw_id,zd_id);
        //获得车牌号列表
        $scope.daozhan={
            cph:'',
            cl_id:''
        };
        $scope.daozhan.cph=$stateParams['cph'];
        //$scope.$on('List.cphList',function(){
        //    var list=CarFactory.getCph();
        //    $scope.cphList=list.ACTION_INFO.RESULT_LIST;
        //    console.log($scope.cphList);
        //});
        SfzFactory.getSfzList(sid,user_id,yh_id,dw_id,zd_id);
        //获得站点列表
        $scope.$on('List.sfzList',function(){
            var list=SfzFactory.getSfz();
            $scope.sfzList=list.ACTION_INFO.RESULT_LIST;
            console.log($scope.sfzList);
        });
        $scope.chooseCph = function (bmmc) {
            $state.go("carlist",{bmmc:bmmc});
        };
        $scope.chooseSfz = function () {
            var alertPopup = $ionicPopup.alert({
                title: '请选择站点',
                scope: $scope,
                template: '<ion-list  ng-repeat="item in sfzList"><ion-radio ng-bind="item.bmmc" style="text-align: center;margin-top: 3px;" ng-click="selectSfz(item.bmmc,item.bm_id)"></ion-radio></ion-list>',
                okText: '取消'
            });
            alertPopup.then(function (res) {
                if (res) {

                } else {

                }
            });
            $scope.selectSfz = function (bmmc, bm_id) {
                $scope.bm_id = bm_id;
                $scope.bmmc = bmmc;
                alertPopup.close();
            };
        };
        var t = new Date();
        var iToDay=t.getDate();
        var iToMon=t.getMonth();
        var iToYear=t.getFullYear();
        var iTDate=new Date(iToYear,iToMon,(iToDay-30));
        $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
        $scope.showStartDate1 = function() { //选择日期
            var optionsStart = {
                date: iTDate,
                mode: 'date',
                maxDate: new Date($scope.endDate) - 10000
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        iTDate=d;
                        $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
                    });
                }
            });
        };

        var endTime = new Date();
        $scope.endDate=$filter('date')(endTime,'yyyy-MM-dd');
        $scope.showEndDate1 = function(){
            console.log('EndDate');
            var optionsEnd = {
                date : endTime,
                mode : 'date',
                minDate: new Date($scope.startDate) - 10000
            };
            datePicker.show(optionsEnd,function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        endTime=d;
                        $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
                    });
                }
            });

        };
        $scope.searchZcd = function(cph,bmmc){
            $state.go('dzreceiptresult',{cph:cph,sfz:bmmc,kssj:$scope.startDate,jssj:$scope.endDate});
        };
    })
    //货损登记详情
    .controller('HsdjDetailCtrl',function($scope,$filter,Storage,HsdjFactory,$stateParams,$ionicLoading,$ionicHistory,$timeout){
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var dw_id=Storage.get('dw_id');
        var bm_id=Storage.get('bm_id');
        var zsxm=Storage.get('zsxm');
        var cyd_id = $stateParams['cyd_id'];


        $scope.hsdj;
        HsdjFactory.getDetailHsdj(sid,user_id,yh_id,cyd_id);
        $scope.$on('Hsdj.detail', function () {
            var detail = HsdjFactory.getDetail();
            $scope.hsdj=detail;
            console.log($scope.hsdj);
            //已做过货损登记则不能修改（从数据库查询）
            $timeout(function(){
                if($scope.hsdj.shsl!=''||$scope.hsdj.dssl!=''||$scope.hsdj.bz!=''){
                    $("#btnOk").removeClass("show").addClass("hide");
                    $("#hssl").attr("disabled", "disabled");
                    $("#hcsl").attr("disabled", "disabled");
                    $("#bz").attr("disabled", "disabled");
                    $("#btnDate").attr("disabled", "disabled");
                }
            },100);
        });


        $scope.ok=function(){
            if($scope.hsdj.dssl==''&&$scope.hsdj.shsl==''){
                $ionicLoading.show({
                    template : '请输入货差数量或货损数量',
                    duration : 1000
                });
            }else {
                HsdjFactory.getHsdj(sid, user_id, yh_id, dw_id, cyd_id, zsxm, $scope.hsdj, $scope.happenDate,bm_id);
                $scope.$on('Hsdj.hsdj', function () {
                    var result = HsdjFactory.getResult();
                    if (result == 000000) {
                        $ionicLoading.show({
                            template: '登记成功',
                            duration: 600
                        });
                        $ionicHistory.goBack();
                    } else {
                            $ionicLoading.show({
                            template: '登记失败',
                            duration: 600
                        });
                    }

                });
            }
        };
        var happenTime = new Date();
        $scope.happenDate=$filter('date')(happenTime,'yyyy-MM-dd');
        $scope.showHappenDate = function(){
            var optionsHappen = {
                date : happenTime,
                mode : 'date'
            };
            datePicker.show(optionsHappen,function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        happenTime=d;
                        $scope.happenDate = $filter('date')(happenTime,'yyyy-MM-dd');
                    });
                }
            });

        };

        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };

    })
